package com.mdd.front.service.impl.zdjd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.zdjd.TZdjdDeviceStateRecord;
import com.mdd.common.mapper.zdjd.TZdjdDeviceStateRecordMapper;
import com.mdd.front.service.zdjd.ITZdjdDeviceStateRecordService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceStateRecordCreateValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceStateRecordSearchValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceStateRecordUpdateValidate;
import com.mdd.front.vo.zdjd.TZdjdDeviceStateRecordDetailVo;
import com.mdd.front.vo.zdjd.TZdjdDeviceStateRecordListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 设备状态变化记录实现类
 * @author LikeAdmin
 */
@Service
public class TZdjdDeviceStateRecordServiceImpl implements ITZdjdDeviceStateRecordService {
        
    @Resource
    TZdjdDeviceStateRecordMapper tZdjdDeviceStateRecordMapper;

    /**
     * 设备状态变化记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TZdjdDeviceStateRecordListedVo>
     */
    @Override
    public PageResult<TZdjdDeviceStateRecordListedVo> list(PageValidate pageValidate, TZdjdDeviceStateRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TZdjdDeviceStateRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tZdjdDeviceStateRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:deviceId@device_id:int",
            "=:deviceUuid@device_uuid:str",
            "=:deviceCode@device_code:str",
            "=:status:int",
            "=:alarmCode@alarm_code:str",
            "=:timeStamp@time_stamp:str",
            "=:longitude:str",
            "=:latitude:str",
        });

        IPage<TZdjdDeviceStateRecord> iPage = tZdjdDeviceStateRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TZdjdDeviceStateRecordListedVo> list = new LinkedList<>();
        for(TZdjdDeviceStateRecord item : iPage.getRecords()) {
            TZdjdDeviceStateRecordListedVo vo = new TZdjdDeviceStateRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 设备状态变化记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TZdjdDeviceStateRecord
     */
    @Override
    public TZdjdDeviceStateRecordDetailVo detail(Integer id) {
        TZdjdDeviceStateRecord model = tZdjdDeviceStateRecordMapper.selectOne(
                new QueryWrapper<TZdjdDeviceStateRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TZdjdDeviceStateRecordDetailVo vo = new TZdjdDeviceStateRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 设备状态变化记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TZdjdDeviceStateRecordCreateValidate createValidate) {
        TZdjdDeviceStateRecord model = new TZdjdDeviceStateRecord();
        model.setDeviceId(createValidate.getDeviceId());
        model.setDeviceUuid(createValidate.getDeviceUuid());
        model.setDeviceCode(createValidate.getDeviceCode());
        model.setStatus(createValidate.getStatus());
        model.setAlarmCode(createValidate.getAlarmCode());
        model.setTimeStamp(createValidate.getTimeStamp());
        model.setLongitude(createValidate.getLongitude().doubleValue());
        model.setLatitude(createValidate.getLatitude().doubleValue());
        tZdjdDeviceStateRecordMapper.insert(model);
    }

    /**
     * 设备状态变化记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TZdjdDeviceStateRecordUpdateValidate updateValidate) {
        TZdjdDeviceStateRecord model = tZdjdDeviceStateRecordMapper.selectOne(
                new QueryWrapper<TZdjdDeviceStateRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setDeviceId(updateValidate.getDeviceId());
        model.setDeviceUuid(updateValidate.getDeviceUuid());
        model.setDeviceCode(updateValidate.getDeviceCode());
        model.setStatus(updateValidate.getStatus());
        model.setAlarmCode(updateValidate.getAlarmCode());
        model.setTimeStamp(updateValidate.getTimeStamp());
        model.setLongitude(updateValidate.getLongitude().doubleValue());
        model.setLatitude(updateValidate.getLatitude().doubleValue());
        tZdjdDeviceStateRecordMapper.updateById(model);
    }

    /**
     * 设备状态变化记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TZdjdDeviceStateRecord model = tZdjdDeviceStateRecordMapper.selectOne(
                new QueryWrapper<TZdjdDeviceStateRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tZdjdDeviceStateRecordMapper.delete(new QueryWrapper<TZdjdDeviceStateRecord>().eq("id", id));
    }

    @Override
    public void insertDeviceStateRecord(TZdjdDeviceStateRecord deviceStateRecord) {
        tZdjdDeviceStateRecordMapper.insert(deviceStateRecord);
    }

    @Override
    public List<TZdjdDeviceStateRecord> selectListByYearMonthDay(Integer year,Integer month,Integer day) {
        QueryWrapper<TZdjdDeviceStateRecord> wrapper = new QueryWrapper<>();
        if(year!=null){
            wrapper.eq("year", year);
        }
        if(month!=null){
            wrapper.eq("month", month);
        }
        if(day!=null){
            wrapper.eq("day", day);
        }
        List<TZdjdDeviceStateRecord> list = tZdjdDeviceStateRecordMapper.selectList(wrapper);
        return Optional.ofNullable(list).orElse(new ArrayList<>());
    }

}
